package cn.javasm.demo;

import cn.javasm.entity.StudyInfo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version: 0.1
 */
public class CreateDocx {

    public static void main(String[] args) throws IOException, TemplateException {
        // 创建配置对象
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_31);
        // 设置编码格式
        configuration.setDefaultEncoding("utf-8");
        // 设置模板的目录
        configuration.setClassLoaderForTemplateLoading(CreateDocx.class.getClassLoader(),"/template");
        // 获取模板
        Template template = configuration.getTemplate("租房合同.xml");
        Map<String,Object> data = new HashMap<>();
        data.put("username","谷丰硕");
        data.put("address","亦庄");
        data.put("date","10月12日");

        List<StudyInfo> list = new ArrayList<>();
        list.add(new StudyInfo("清华大学","2019-09-01","2023-06-01"));
        list.add(new StudyInfo("枫杨外国语学校","2016-09-1","2019-06-01"));
        list.add(new StudyInfo("实验一中","2013-09-1","2016-06-01"));

        data.put("list",list);
        // 把数据渲染到模板上，并且输出到本地
        template.process(data,new FileWriter("D:\\租房合同.docx", StandardCharsets.UTF_8));

    }

}
